package awt.awt04;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class menuDemo01 {
    private Frame fr=new Frame("测试PopupMenu");

    TextArea ta=new TextArea("爱我中华",6,40);

    Panel p=new Panel();

    PopupMenu pMenu=new PopupMenu();

    MenuItem comment=new MenuItem("注释");
    MenuItem cancelComment=new MenuItem("取消注释");
    MenuItem copy=new MenuItem("复制");
    MenuItem save=new MenuItem("保存");


    public void init(){

        ActionListener listener=new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                String actionCommand=e.getActionCommand();
                ta.append("\n"+"您点击了："+actionCommand);
            }
        };

        comment.addActionListener(listener);
        cancelComment.addActionListener(listener);
        copy.addActionListener(listener);
        save.addActionListener(listener);


        pMenu.add(comment);
        pMenu.add(cancelComment);
        pMenu.add(copy);
        pMenu.add(save);

        p.add(pMenu);


        // 设置panel的大小
        p.setPreferredSize(new Dimension(400,300));


        p.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                // 是否触发PopupMenu
                boolean flag=e.isPopupTrigger();
                if(flag){
                    pMenu.show(p,e.getX(),e.getY());
                }
            }
        });

        fr.add(ta,BorderLayout.NORTH);
        fr.add(p);

        fr.pack();
        fr.setVisible(true);

    }

    public static void main(String[] args){
        new menuDemo01().init();
    }

}
